package com.ruibo.modules.common.service.upload.impl;

import com.ruibo.modules.common.service.upload.FileUpload;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;

/**
 * @program: ruibo-parent
 * @description:
 * @author: Sun Feng
 * @create: 2020-06-30 16:27
 **/
@Component(value = "fastDFSFileUpload")
public class FastDFSFileUpload implements FileUpload {

    @Resource
    private FastFileStorageClient storageClient;

    @Autowired
    private ThumbImageConfig thumbImageConfig;

    @Value("${fdfs.reqHost}")
    private String reqHost;

    @Value("${fdfs.reqPort}")
    private String reqPort;

    @Override
    public String upload(MultipartFile file, String extName) throws IOException {
        StorePath storePath = storageClient.uploadFile((InputStream)file.getInputStream(),
                file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);

        // 缩略图
        //String path = thumbImageConfig.getThumbImagePath(storePath.getPath()) ;


        return   "http://" + reqHost + ":" + reqPort + "/" + storePath.getFullPath();
    }
}
